package org.ilead.iserve.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.ilead.iserve.constants.CookiesKeyConstant;

import com.opensymphony.xwork2.ActionContext;

public class CookiesHelper {
	
	public static void setUserCookies(HttpServletRequest request, HttpServletResponse response, String username, String password){
		setCookie(request, response, CookiesKeyConstant.LOP_LOGIN_NAME, username);
		setCookie(request, response, CookiesKeyConstant.LOP_AUOT_LOGIN_FLAG, "true");
		setCookie(request, response, CookiesKeyConstant.LOP_LOGIN_PASSWORD, password);
	}
	
	public static void clearUserCookies(HttpServletRequest request, HttpServletResponse response){
		clearCookieByName(request, response, CookiesKeyConstant.LOP_AUOT_LOGIN_FLAG);
		clearCookieByName(request, response, CookiesKeyConstant.LOP_LOGIN_NAME);
		clearCookieByName(request, response, CookiesKeyConstant.LOP_LOGIN_PASSWORD);
	}
	
	public static Cookie getCookieByName(HttpServletRequest request, String name){
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name)){
					return cookie;
				}
			}
		}
		
		return null;
	}
	
	public static void setCookie(HttpServletRequest request, HttpServletResponse response,String name, String value){
		String host = request.getServerName();
		Cookie cookie = new Cookie(name, value); // 保存用户名到Cookie
		cookie.setPath(request.getContextPath());
		cookie.setDomain(host);
		cookie.setMaxAge(99999999);
		response.addCookie(cookie);
	}
	
	public static String getCookieValueByName(HttpServletRequest request, String name){
		Cookie cookie = getCookieByName(request, name);
		return cookie != null ? cookie.getValue() : "";
	}
	
	public static void clearCookieByName(HttpServletRequest request, HttpServletResponse response, String name){
		String host = request.getServerName();
		Cookie cookie = getCookieByName(request, name);
		if(cookie == null)
			return;
		
		cookie.setPath(request.getContextPath());
		cookie.setMaxAge(0);
		cookie.setDomain(host);
		response.addCookie(cookie);
	}
	
	/**
	 * 根据名称，判断是否有该名称的Cookie,若Cookie的值为null或""也认为没有该Cookie
	 * @param name
	 * @return
	 */
	public static boolean hasCookie(String name){
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name) && cookie.getValue() != null && !"".equals(cookie.getValue())){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public static boolean hasCookie(HttpServletRequest request, String name){
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name) && cookie.getValue() != null && !"".equals(cookie.getValue())){
					return true;
				}
			}
		}
		
		return false;
	}
}
